//
//  XLGDownloadManager+ClearSameDownloadIndex.m
//  etionCBLogic
//
//  Created by wu jingxing on 13-9-3.
//  Copyright (c) 2013年 GuangZhouXuanWu. All rights reserved.
//

#import "XLGDownloadManager+ClearSameDownloadIndex.h"

#import "XLGDownloadManagerInternal.h"

@implementation XLGDownloadManager (ClearSameDownloadIndex)

- (void)clearExistSameDownloadItemWithURLs:(NSArray *)arURL asyncCompletion:(void(^)(NSArray *arResult))completion
{
    [self deleteDownloadItemWithURLs:arURL asyncCompletion:completion];
}

- (void)clearExistSameDownloadItemWithExtendIDs:(NSArray *)arExtendids asyncCompletion:(void(^)(NSArray *arResult))completion
{
    [self deleteDownloadItemWithExtendIDs:arExtendids asyncCompletion:completion];
}

- (void)clearExistSameFinishedFileWithExtendIDs:(NSArray *)arGUID asyncCompletion:(void (^)(NSArray *guid))completion
{
    DispatchThread(^
    {
        if(arGUID.count > 0)
        {
            NSMutableArray *arDel = [[NSMutableArray new] autorelease_mm];

            [_commonindex getIndexWithExtendIDs:arGUID completion:^(NSInteger nResult, NSArray *index)
            {
                for(XLGDownloadIndex *i in index)
                {
                    [arDel addObject:i.extendID];
                }
            }];

            if(arDel.count > 0)
                [self deleteFinishedItemWithExtendIDs:arDel asyncCompletion:completion];
            else
                completion(arDel);
        }
        else
            completion(nil);
    });
}

- (void)clearExistSameFinishedFileWithURLs:(NSArray *)arURL asyncCompletion:(void (^)(NSArray *URL))completion
{
    DispatchThread(^
    {
        if(arURL.count > 0)
        {
            NSMutableArray *arDel = [[NSMutableArray new] autorelease_mm];

            [_commonindex getIndexWithURLs:arURL completion:^(NSInteger nResult, NSArray *index)
            {
                 for(XLGDownloadIndex *i in index)
                 {
                     [arDel addObject:i.extendID];
                 }
             }];
            
            if(arDel.count > 0)
                [self deleteFinishedItemWithURLs:arDel asyncCompletion:completion];
            else
                completion(arDel);
        }
        else
            completion(nil);
    });
}

- (NSArray *)checkExistSameDownloadTaskWithURLs:(NSArray *)arURL
{
    NSMutableArray *arGuid = [[NSMutableArray new] autorelease_mm];

    [_dlingIndexQueue dispatchThreadSync:^
    {
        for (NSString *http in arURL)
        {
            for (XLGDownloadOperation *o in _arDlingindex)
            {
                XLGDownloadIndex *mi = o.mi;
                if ([http isEqualToString:mi.URL])
                {
                    [arGuid addObject:http];
                    break;
                }
            }
        }
    }];

    return arGuid.count > 0 ? arGuid : nil;
}

- (NSArray *)checkExistSameFinishedFileWithFileName:(NSArray *)arFilename
{
    NSMutableArray *arURL = [[NSMutableArray new] autorelease_mm];

    [_dlingIndexQueue dispatchThreadSync:^
    {
        [_commonindex getFinishedIndexCompletion:^(NSInteger nResult, NSArray *index)
        {
            for(XLGDownloadIndex *i in index)
            {
                for(NSString *fileName in arFilename)
                {
                    if([i.URL rangeOfString:fileName].location != NSNotFound)
                    {
                        [arURL addObject:i.URL];
                        break;
                    }
                }
            }
        }];
    }];
    
    return arURL.count > 0 ? arURL : nil;
}

@end
